﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class admin_Product_Addproduct : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
   
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
        string str = "insert into  TheStorage (ID_Product,ProductName,Quantity,Price,Product_SaleOff,Details,Image,TypeProduct_ID)values(@id,@name,@quan,@Price,@prosale,@Details,@img,@typeProduct)";
        SqlCommand cmd = new SqlCommand(str, conn);

        string id = Convert.ToString(Guid.NewGuid());


         string a= id.Substring(0, 8);

        cmd.Parameters.Add("id",ddlCateName.SelectedItem.Value+a);
        cmd.Parameters.Add("@name", txtProductName.Text);
        cmd.Parameters.Add("@quan", txtQuantity.Text);
        cmd.Parameters.Add("@Price", txtPrice.Text);
        cmd.Parameters.Add("@prosale", txtProductSale.Text);
        cmd.Parameters.Add("@Details", txtDetails.Text);
        cmd.Parameters.Add("@typeProduct", ddlCateName.SelectedItem.Value);
        if (fuImg.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(fuImg.FileName);
                fuImg.SaveAs(Server.MapPath(".\\Image\\") + filename);
                Response.Write("Đã tải lên tệp " + " " + filename);
                cmd.Parameters.Add("img", "~/admin/Product/image/" + filename);
            }
            catch (Exception ex)
            {
                Response.Write("Lỗi"+ex);
            }
        }








        conn.Open();
        cmd.ExecuteNonQuery();
     
        Response.Write("thanh cong");

        //gvLoaiSP.EditIndex = -1;
       // gvLoaiSP.DataBind();
       // gvLoaiSP.Visible = true;
       // Response.Write("chuc mung ban da add thanh cong");
    }

   
    protected void ddlCateName_SelectedIndexChanged(object sender, EventArgs e)
    {

        /*SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingOnlineConnectionString"].ConnectionString);
        string st = "select * from TheStorage";

        SqlCommand cmd1 = new SqlCommand(st, conn1);





        int i = 1111;
        i++;
        conn1.Open();
        SqlDataReader dr = cmd1.ExecuteReader();
        string b = ddlCateName.SelectedItem.Value + i;
        while (dr.Read())
        {

            string contact = (string)dr["ID_Product"];
            if (b != contact)
            {
                for (int y = i; y <= 2000; y++)
                {
                    int k = y;
                    k++;
                    
                    string c=ddlCateName.SelectedItem.Value + k;
                    txtDetails.Text=c;
                    Session["s"] = c;
                }
            }
        }



        conn1.Close();
         */
    }
   
}
